package section_06.regularExpression;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TheReplacements {
	public static void main(String[] args) throws Exception {
		String s = read("txt/replacements.txt");
//		System.out.println(s);
		Matcher mInput = Pattern.compile("/\\*!(.*)!\\*/", Pattern.DOTALL).matcher(s);
		if(mInput.find())
			s = mInput.group(1);
		s = s.replaceAll(" {2,}", "");
		s = s.replaceAll("(?m)^ +", "");
		System.out.println(s);
		s = s.replaceFirst("[aeiou]", "(VOWEL1)");
		StringBuffer sbuf = new StringBuffer();
		Pattern p = Pattern.compile("[aeiou]");
		Matcher m = p.matcher(s);
		while(m.find())
			m.appendReplacement(sbuf, m.group().toUpperCase());
		m.appendTail(sbuf);
		System.err.println(sbuf);
		
	}

	private static String read(String path) throws FileNotFoundException, IOException {
		BufferedReader reader = new BufferedReader(new FileReader(path));
		String len;
		StringBuilder sb = new StringBuilder();
		while((len=reader.readLine())!=null){
			sb.append(len);
			sb.append("\r\n");
		}
		reader.close();
		return sb.toString();
	}
}
